<?
/**
* Use $_SESSION["GDate"] if you are using custom date
* or just use the system date
*
* $today_date = $_SESSION["GDate"];
* $today_date = date("d-m-Y");
*
*/
$today_date		  = date("d-m-Y");

$thisSequence   = "SN_SEQ";
$jobIdTag       = "45";

$srcDbHdr      = "SN_HDR";
$srcDbDtl      = "SN_DTL";
$srcDbHdrLog   = "LOG_SN_HDR";
$srcDbSequence = "MST_SEQUENCE";

$UID     = unserialize($_SESSION["serUID"]);

$userId  = $UID->getUID();

$Db = new oracleDb;
$Db->init($_SESSION["ORCL"],
           $_SESSION["OPASS"],
           $_SESSION["SID"],
           _NO_COMMENT);

if($Db->connect()){

	$thisPeriode = &$_SESSION["sysPeriode"];

	/**
	* Fetch the sequence number
	* and below is the how to
	*
	* 1. we fetch the current number
	* 2. we update the current number
	* 3. if the fetched number as condition is
	*    valid then there is no interuption while
	*    updating
	*
	*/

	$seqEditStatus = false;

	while($seqEditStatus == false){

  	$CFetchers->fetchCurSequence($Db,
                	               $thisSequence,
                	               $thisPeriode,
                	               $srcDbSequence,
                	               $srcJidIndex);

  	$CHandlers->initCurSequence($Db,
                	              $thisSequence,
                	              $srcJidIndex,
                	              $thisPeriode,
                	              $srcDbSequence,
                	              $seqEditStatus);
  }

  /**
  * Extract the system period
  *
  */
  $tmpDate = explode("-", $thisPeriode);
  $wrkDate = $tmpDate[0]. /* This period month */
             "-".
             $tmpDate[1]; /* This period year */

  $jobId  = addDigit((string) $srcJidIndex).
            $srcJidIndex.
            "/" .
            $jobIdTag.
            "/" .
            $wrkDate;

  $hdrDate = "TO_DATE('".$_POST["trDateEnt"]."' , 'dd-mm-YYYY')";

  for($iRow = 0; $iRow < $dataup ; $iRow++){

    $CFetchers->fetchModelId($Db,
                             $_POST["modelEnt"][$iRow],
                             $SourceDb->fetchValue("srcDbModel"),
                             $modelId);

    $CFetchers->fetchIsSerNumber($Db,
                                 $_POST["modelEnt"][$iRow],
                                 $modelId,
                                 $SourceDb->fetchValue("srcDbModel"),
                                 $isSerNumber);

    if($isSerNumber){

      $values = " '".$jobId."',     ".
                "  ".($iRow+1)." ,  ".
                " '".$modelId."',   ".
                "  ".$_POST["qtyEnt"][$iRow]." ,    ".
                "  ".$_POST["headEnt"][$iRow]." ,   ".
                "  ".$_POST["lengthEnt"][$iRow]." , ".
                " '".$_POST["serNumEnt"][$iRow]."'  ";
    }else{

      $values = " '".$jobId."',     ".
                "  ".($iRow+1)." ,  ".
                " '".$modelId."',   ".
                "  ".$_POST["qtyEnt"][$iRow]." ,    ".
                "  ".$_POST["headEnt"][$iRow]." ,   ".
                "  ".$_POST["lengthEnt"][$iRow]."   ";
    }

    $CLHandlers->insToDtl($Db,
                          $values,
                          $isSerNumber,
                          $SourceDb->fetchValue("srcDbDtl"));
  }

  $CFetchers->fetchHdrNo($Db,
                         $_POST["trDateEnt"],
                         $srcDbHdr,
                         $hdrNo);

  $values = " '".$jobId."',   ".            /* JobID      */
            " ' ', ".                       /* Reference  */
            "  ".$hdrDate." , ".            /* TR_DATE    */
            "  ".$hdrNo." ,   ".            /* hdr Number */
            " '".$_POST["srcWhs"]."', ".    /* Source WHS */
            " '".$_POST["notaSrcEnt"]."',". /* Nota source*/
            " '"._NOT_ACTIVE."' ";          /* Status     */

  $CLHandlers->insToHdr($Db,
                        $values,
                        $SourceDb->fetchValue("srcDbHdr"));

  /**
  * Insert Log
  *
  */
  $CHandlers->insToDbLog($Db,
                         $today_date,
                         $jobId,
                         $userId,
                         $_SERVER["REMOTE_ADDR"],
                         _ENTRY_MODE,
                         $srcDbHdrLog);

	$Db->commit();

	if($Db->getStatus()){

	  $Misc->usrMsg("Upload Complete");
	}else{

	  $Misc->usrDieJs("Upload Process Interupted");
	}

	$Db->dc();

	/**
	* Pending...
	* I'm not sure if this
	* reset procedure off
	* causes buggy
	*
	*	$restore  = 0;
	* $uprun    = 0;
	* $dataup   = 0;
	*
	*/

	//jumpTo(2,"../".$_SESSION["source_caller"]);

}else{

	$Misc->usrDieJs("Db connection failed");
}

?>